/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddUploadTaskView.java
 *
 * Created on 2011-06-03, 19:27:30
 */

package ytmc.uploader;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;
import org.apache.commons.lang.StringUtils;
import ytmc.DialogView;
import ytmc.setup.*;

/**
 *
 * @author K
 */
public class AddUploadTasksView extends DialogView
{
    public String getVideoTitle()
    {
        return titleField.getText();
    }

    public void setVideoTitle(String title)
    {
        titleField.setText(title);
    }

    public VideoCategory getVideoCategory()
    {
        return VideoCategory.fromString(categoryCombobox.getSelectedItem().toString());
    }

    public void setVideoCategory(VideoCategory category)
    {
        categoryCombobox.setSelectedItem(category.getFriendlyName());
    }

    public String getVideoTags()
    {
        return tagsField.getText();
    }

    public void setVideoTags(String tags)
    {
        tagsField.setText(tags);
    }

    public String getVideoDescription()
    {
        return descriptionTexarea.getText();
    }

    public void setVideoDescription(String description)
    {
        descriptionTexarea.setText(description);
    }

     public void canAccept(boolean canAccept)
    {
        addBtn.setEnabled(canAccept);
    }


    public void clear()
    {
        titleField.setText("");
        tagsField.setText("");
        categoryCombobox.setSelectedIndex(0);
        descriptionTexarea.setText("");
    }

    private UploaderController controller;
    private AddUploadTasksModel model;

    /** Creates new form AddUploadTaskView */
    public AddUploadTasksView(UploaderController controller, AddUploadTasksModel model)
    {
        this.controller = controller;
        this.model = model;

        initComponents();
        fillCategories();
        setLocationRelativeTo(null);

        tasksToAdd.setModel(model);

        model.addPropertyChangeListener(new PropertyChangeListenerImpl());
        titleField.getDocument().addDocumentListener(new TitleListener());
        categoryCombobox.addItemListener(new CategoryListener());
        tagsField.getDocument().addDocumentListener(new TagsListener());
        descriptionTexarea.getDocument().addDocumentListener(new DescriptionListener());
        tasksToAdd.addListSelectionListener(new ListSelectionListenerImpl());
    }

    private void fillCategories()
    {
        for(VideoCategory cat : VideoCategory.values())
        {
            categoryCombobox.addItem(cat.getFriendlyName());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titleField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tagsField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        categoryCombobox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTexarea = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        addBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tasksToAdd = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        showFileDialogBtn = new javax.swing.JButton();
        showFileDialogBtn1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Dodaj nowe zadanie wysyłania");
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setResizable(false);

        jLabel2.setText("Tytuł:");

        jLabel3.setText("Tagi:");

        jLabel4.setText("Kategoria:");

        jLabel5.setText("Opis");

        descriptionTexarea.setColumns(20);
        descriptionTexarea.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
        descriptionTexarea.setRows(5);
        jScrollPane1.setViewportView(descriptionTexarea);

        addBtn.setText("Dodaj");
        addBtn.setEnabled(false);
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });
        jPanel1.add(addBtn);

        cancelBtn.setText("Anuluj");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });
        jPanel1.add(cancelBtn);

        tasksToAdd.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tasksToAdd);

        jLabel6.setText("Pliki wideo:");

        showFileDialogBtn.setText("+");
        showFileDialogBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showFileDialogBtnActionPerformed(evt);
            }
        });
        jPanel2.add(showFileDialogBtn);

        showFileDialogBtn1.setText("X");
        showFileDialogBtn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showFileDialogBtn1ActionPerformed(evt);
            }
        });
        jPanel2.add(showFileDialogBtn1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 292, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(categoryCombobox, 0, 303, Short.MAX_VALUE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2)
                            .addComponent(titleField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                            .addComponent(tagsField, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tagsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(categoryCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelBtnActionPerformed
    {//GEN-HEADEREND:event_cancelBtnActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addBtnActionPerformed
    {//GEN-HEADEREND:event_addBtnActionPerformed
        controller.addTasks();
        this.setVisible(false);
    }//GEN-LAST:event_addBtnActionPerformed

    private void showFileDialogBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showFileDialogBtnActionPerformed
    {//GEN-HEADEREND:event_showFileDialogBtnActionPerformed
        JFileChooser openFileDialog = new JFileChooser();
        openFileDialog.setAcceptAllFileFilterUsed(false);
        openFileDialog.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        openFileDialog.setCurrentDirectory(FileSystemView.getFileSystemView().getHomeDirectory());
        openFileDialog.setDialogType(JFileChooser.OPEN_DIALOG);
        openFileDialog.setFileFilter(new FileFilterImpl());
        openFileDialog.setDialogTitle("Wybierz plik wideo lub katalog");

        int result = openFileDialog.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION)
        {
            String path = openFileDialog.getSelectedFile().getAbsolutePath();
            controller.addTasksFromPath(path);
        }
    }//GEN-LAST:event_showFileDialogBtnActionPerformed

    private void showFileDialogBtn1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showFileDialogBtn1ActionPerformed
    {//GEN-HEADEREND:event_showFileDialogBtn1ActionPerformed
        controller.removeSelectedTask();
    }//GEN-LAST:event_showFileDialogBtn1ActionPerformed

    private class FileFilterImpl extends FileFilter
    {
        private Collection<String> extensions;

        public FileFilterImpl()
        {
            extensions = Arrays.asList( VideoFormat.friendlyNames() );
        }

        @Override
        public boolean accept( File file )
        {
            if( file.isDirectory() )
            {
                return true;
            }

            String name = file.getName().toLowerCase();
            for( String extenstion : extensions )
            {
                if( name.endsWith( "." + extenstion ) )
                {
                    return true;
                }
            }

            return false;
        }

        @Override
        public String getDescription()
        {
            return "Pliki wideo (*." + StringUtils.join( extensions, "|*." ) + ")";
        }
    }

    private class TitleListener implements DocumentListener
    {

        @Override
        public void insertUpdate(DocumentEvent e)
        {
            controller.setTitle(titleField.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e)
        {
            controller.setTitle(titleField.getText());
        }

        @Override
        public void changedUpdate(DocumentEvent e)
        {
            controller.setTitle(titleField.getText());
        }
    }

    private class CategoryListener implements ItemListener
    {
        @Override
        public void itemStateChanged(ItemEvent e)
        {
            VideoCategory category = VideoCategory.fromString(categoryCombobox.getSelectedItem().toString());
            controller.setCategory(category);
        }
    }

    private class TagsListener implements DocumentListener
    {
        @Override
        public void insertUpdate(DocumentEvent e)
        {
            controller.setTags(tagsField.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e)
        {
            controller.setTags(tagsField.getText());
        }

        @Override
        public void changedUpdate(DocumentEvent e)
        {
            controller.setTags(tagsField.getText());
        }
    }

    private class DescriptionListener implements DocumentListener
    {
        @Override
        public void insertUpdate(DocumentEvent e)
        {
            controller.setDescription(descriptionTexarea.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e)
        {
            controller.setDescription(descriptionTexarea.getText());
        }

        @Override
        public void changedUpdate(DocumentEvent e)
        {
            controller.setDescription(descriptionTexarea.getText());
        }
    }

    private class PropertyChangeListenerImpl implements PropertyChangeListener
    {
        @Override
        public void propertyChange(PropertyChangeEvent evt)
        {
            if (evt.getPropertyName().equals("selectedTask"))
            {
                if (evt.getNewValue() != null)
                {
                    UploadTask selectedTask = (UploadTask) evt.getNewValue();
                    tasksToAdd.setSelectedValue(selectedTask.getFilePath(), true);
                    titleField.setText(selectedTask.getTitle());
                    categoryCombobox.setSelectedItem(selectedTask.getCategory().getFriendlyName());
                    tagsField.setText(selectedTask.getTags());
                    descriptionTexarea.setText(selectedTask.getDescription());
                }
            }
        }
    }

    private class ListSelectionListenerImpl implements ListSelectionListener
    {
        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            controller.setSelectedTask(tasksToAdd.getSelectedIndex());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JComboBox categoryCombobox;
    private javax.swing.JTextArea descriptionTexarea;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton showFileDialogBtn;
    private javax.swing.JButton showFileDialogBtn1;
    private javax.swing.JTextField tagsField;
    private javax.swing.JList tasksToAdd;
    private javax.swing.JTextField titleField;
    // End of variables declaration//GEN-END:variables

}
